home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH13 / FILEMACS.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-24  |  5.2 KB  |  296 lines

  1. ; FileMacs.asm
  2. ;
  3. ; This program presents a set of macros that make file I/O with the 
  4. ; Standard Library even easier to do.
  5. ;
  6. ; The main program writes a multiplication table to the file "MyFile.txt".
  7.  
  8.         .xlist
  9.         include     stdlib.a
  10.         includelib    stdlib.lib
  11.         .list
  12.  
  13.  
  14. dseg        segment    para public 'data'
  15.  
  16. CurOutput    dword    ?
  17.  
  18. Filename    byte    "MyFile.txt",0
  19.  
  20. i        word    ?
  21. j        word    ?
  22.  
  23. TheFile        filevar    {}
  24.  
  25. dseg        ends
  26.  
  27.  
  28. cseg        segment    para public 'code'
  29.         assume    cs:cseg, ds:dseg
  30.  
  31.  
  32. ; For-Next macros from Chapter Eight.
  33. ; See Chapter Eight for details on how this works.
  34.  
  35. ForLp        macro    LCV, Start, Stop
  36.         local    ForLoop
  37.  
  38.         ifndef    $$For&LCV&
  39. $$For&LCV&    =    0
  40.         else
  41. $$For&LCV&    =    $$For&LCV& + 1
  42.         endif
  43.  
  44.         mov    ax, Start
  45.         mov    LCV, ax
  46.  
  47. ForLoop        textequ    @catstr($$For&LCV&, %$$For&LCV&)
  48. &ForLoop&:
  49.         mov    ax, LCV
  50.         cmp    ax, Stop
  51.         jg    @catstr($$Next&LCV&, %$$For&LCV&)
  52.         endm
  53.  
  54.  
  55.  
  56. Next        macro    LCV
  57.         local    NextLbl
  58.         inc    LCV
  59.         jmp    @catstr($$For&LCV&, %$$For&LCV&)
  60. NextLbl        textequ    @catstr($$Next&LCV&, %$$For&LCV&)
  61. &NextLbl&:
  62.         endm
  63.  
  64.  
  65. ; File I/O macros:
  66. ;
  67. ;
  68. ; SetPtr sets up the CurOutput pointer variable.  This macro is called
  69. ; by the other macros, it's not something you would normally call directly.
  70. ; Its whole purpose in life is to shorten the other macros and save a little
  71. ; typing.
  72.  
  73. SetPtr        macro    fvar
  74.         push    es
  75.         push    di
  76.  
  77.         mov    di, offset fvar
  78.         mov    word ptr CurOutput, di
  79.         mov    di, seg fvar
  80.         mov    word ptr CurOutput+2, di
  81.  
  82.         PushOutAdrs
  83.         lesi    FileOutput
  84.         SetOutAdrs
  85.         pop    di
  86.         pop    es
  87.         endm
  88. ;
  89. ;
  90. ;
  91. ; fprint-    Prints a string to the display.
  92. ;
  93. ; Usage:
  94. ;        fprint    filevar,"String or bytes to print"
  95. ;
  96. ; Note: you can supply optional byte or string data after the string above by
  97. ;    enclosing the data in angle brackets, e.g., 
  98. ;
  99. ;        fprint    filevar,<"string to print",cr,lf>
  100. ;
  101. ; Do *NOT* put a zero terminating byte at the end of the string, the fprint macro
  102. ; will do that for you automatically.
  103.  
  104. fprint        macro    fvar:req, string:req
  105.         SetPtr    fvar
  106.  
  107.         print
  108.         byte    string
  109.         byte    0
  110.  
  111.         PopOutAdrs
  112.         endm
  113.  
  114. ; fprintf-    Prints a formatted string to the display.
  115. ; fprintff-    Like fprintf, but handles floats as well as other items.
  116. ;
  117. ; Usage:
  118. ;        fprintf     filevar,"format string", optional data values
  119. ;        fprintff filevar,"format string", optional data values
  120. ; Examples:
  121. ;
  122. ;    fprintf     FileVariable,"i=%d, j=%d\n", i, j
  123. ;    fprintff FileVariable,"f=%8.2f, i=%d\n", f, i
  124. ;
  125. ; Note: if you want to specify a list of strings and bytes for the format string,
  126. ;       just surround the items with an angle bracket, e.g.,
  127. ;
  128. ;    fprintf FileVariable, <"i=%d, j=%d",cr,lf>, i, j
  129. ;
  130. ;
  131.  
  132. fprintf        macro    fvar:req, FmtStr:req, Operands:vararg
  133.         setptr    fvar
  134.  
  135.         printf
  136.         byte    FmtStr
  137.         byte    0
  138.  
  139.         for    ThisVal, <Operands>
  140.         dword    ThisVal
  141.         endm
  142.  
  143.         PopOutAdrs
  144.         endm
  145.  
  146. fprintff    macro    fvar:req, FmtStr:req, Operands:vararg
  147.         setptr    fvar
  148.  
  149.         printff
  150.         byte    FmtStr
  151.         byte    0
  152.  
  153.         for    ThisVal, <Operands>
  154.         dword    ThisVal
  155.         endm
  156.  
  157.         PopOutAdrs
  158.         endm
  159.  
  160.  
  161. ; F-    This is a generic macro that converts stand-alone (no code stream parameters)
  162. ;    stdlib functions into file output routines.  Use it with putc, puts, puti,
  163. ;    putu, putl, putisize, putusize, putlsize, putcr, etc.
  164. ;
  165. ; Usage:
  166. ;
  167. ;    F    StdLibFunction, FileVariable
  168. ;
  169. ; Examples:
  170. ;
  171. ;    mov    al, 'A'
  172. ;    F    putc, TheFile
  173. ;    mov    ax, I
  174. ;    mov    cx, 4
  175. ;    F    putisize, TheFile
  176.  
  177.  
  178. F        macro    func:req, fvar:req
  179.         setptr    fvar
  180.         func
  181.         PopOutAdrs
  182.         endm        
  183.  
  184. ; WriteLn- Quick macro to handle the putcr operation (since this code calls putcr
  185. ; so often).
  186.  
  187. WriteLn        macro    fvar:req
  188.         F    putcr, fvar
  189.         endm
  190.  
  191.  
  192. ; FileOutput- Writes the single character in AL to an output file.
  193. ; The macros above redirect the standard output to this routine
  194. ; to print data to a file.
  195.  
  196. FileOutput    proc    far
  197.         push    es
  198.         push    di
  199.         push    ds
  200.         mov    di, dseg
  201.         mov    ds, di
  202.  
  203.         les    di, CurOutput
  204.         fputc
  205.  
  206.         pop    ds
  207.         pop    di
  208.         pop    es
  209.         ret
  210. FileOutput    endp
  211.  
  212.  
  213. ; A simple main program that tests the code above.
  214. ; This program writes a multiplication table to the file "MyFile.txt"
  215.  
  216. Main        proc
  217.         mov    ax, dseg
  218.         mov    ds, ax
  219.         mov    es, ax
  220.         meminit
  221.  
  222. ; Rewrite(TheFile, FileName);
  223.  
  224.         ldxi    FileName
  225.         lesi    TheFile
  226.         fcreate
  227.  
  228. ; writeln(TheFile);
  229. ; writeln(TheFile,'    ');
  230. ; for i := 0 to 5 do write(TheFile,'|',i:4,' ');
  231. ; writeln(TheFile);
  232.  
  233.         WriteLn    TheFile
  234.         fprint    TheFile,"    "
  235.  
  236.         forlp    i,0,5
  237.         fprintf    TheFile, "|%4d ", i
  238.         next    i
  239.         WriteLn    TheFile
  240.  
  241. ; for j := -5 to 5 do begin
  242. ;
  243. ;    write(TheFile,'----');
  244. ;    for i := 0 to 5 do write(TheFile, '+-----');
  245. ;    writeln(TheFile);
  246. ;
  247. ;    write(j:3, ' |');
  248. ;    for i := 0 to 5 do write(i*j:4, ' |);
  249. ;    writeln(TheFile);
  250. ;
  251. ; end;
  252.  
  253.         forlp    j,-5,5
  254.  
  255.         fprint    TheFile,"----"
  256.         forlp    i,0,5
  257.         fprintf    TheFile,"+-----"
  258.         next    i
  259.         fprint    TheFile,<"+",cr,lf>
  260.  
  261.         fprintf    TheFile, "%3d |", j
  262.  
  263.         forlp    i,0,5
  264.  
  265.         mov    ax, i
  266.         imul    j
  267.         mov    cx, 4
  268.         F    putisize, TheFile
  269.         fprint    TheFile, " |"
  270.  
  271.         next    i
  272.         Writeln    TheFile
  273.  
  274.         next    j
  275.         WriteLn    TheFile
  276.  
  277. ; Close(TheFile);
  278.  
  279.         lesi    TheFile
  280.         fclose
  281.  
  282.  
  283. Quit:        ExitPgm            ;DOS macro to quit program.
  284. Main        endp
  285.  
  286. cseg            ends
  287.  
  288. sseg        segment    para stack 'stack'
  289. stk        db    1024 dup ("stack   ")
  290. sseg        ends
  291.  
  292. zzzzzzseg    segment    para public 'zzzzzz'
  293. LastBytes    db    16 dup (?)
  294. zzzzzzseg    ends
  295.         end    Main
  296.